<template>
   <el-card>
        <el-form :model="queryParams" ref="queryParams" :inline="true" label-width="120px">
            <el-form-item label="网关分组：" prop="gatewayGroup">
               <el-select v-model="queryParams.gatewayGroup" placeholder="请选择" size="small">
                    <el-option v-for="item in gatewayGroupList" :key="item.groupId" :label="`${item.groupName}(${item.groupId})`" :value="item.groupId"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="回调方式：" prop="callbackType">
                <el-select v-model="queryParams.callbackType" placeholder="请选择" size="small">
                    <el-option label="同步" value="a"></el-option>
                    <el-option label="异步" value="s"></el-option>
                </el-select>
            </el-form-item>
             <el-form-item label="前端服务名：" prop="service">
                <el-input v-model="queryParams.service" placeholder="前端服务名" size="small"></el-input>
            </el-form-item>
             <el-form-item label="前端路径：" prop="path">
                <el-input v-model="queryParams.path" placeholder="前端路径" size="small"></el-input>
            </el-form-item>
             <el-form-item label="请求方法：" prop="methods">
                <el-select v-model="queryParams.methods" placeholder="请选择" size="small" multiple collapse-tags>
                    <el-option v-for="item in methods" :label="item" :key="item" :value="item"></el-option>
                  </el-select>
            </el-form-item>
             <el-form-item label="插件" prop="plugins">
                <el-select v-model="queryParams.plugins" placeholder="请选择" size="small" multiple collapse-tags>
                    <el-option v-for="item in pluginList" :label="item.chnName" :key="item.id" :value="item.id"></el-option>
                  </el-select>
            </el-form-item>
             <el-form-item label="访问" prop="access">
                <el-select v-model="queryParams.access" placeholder="请选择" size="small">
                    <el-option label="允许" value="a"></el-option>
                    <el-option label="禁止" value="f"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item >
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-delete" size="small" @click="resetQuery('queryParams')">清空</el-button>
            </el-form-item>
        </el-form>
         <el-row :gutter="10">
            <el-col :span="24">
                <div style="margin-bottom: 10px">
                    <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
                </div>
            </el-col>
        </el-row>
        <el-table :data="tableData" border style="width: 100%" size="small">
            <el-table-column type="index" label="#" width="50"></el-table-column>
            <el-table-column label="网关分组" prop="gatewayGroup">
                <template slot-scope="{row}">
                    <span>{{ row.gatewayGroup | groupFilter(gatewayGroupList) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="回调方式" prop="callbackType">
                <template slot-scope="{row}">
                    <span v-if="row.callbackType==='s'">同步</span>
                    <span v-if="row.callbackType==='a'">异步</span>
                </template>
            </el-table-column>
            <el-table-column label="前端服务名" prop="service"></el-table-column>
            <el-table-column label="前端路径" prop="path"></el-table-column>
            <el-table-column label="请求方法" prop="method"></el-table-column>
            <el-table-column label="插件" prop="pluginNames"></el-table-column>
            <el-table-column label="访问" prop="access" width="100">
                 <template slot-scope="{row}">
                    <el-tag type="success" v-if="row.access==='a'">允许</el-tag>
                    <el-tag v-if="row.access==='f'">禁止</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row.id)">查看</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row.id)">编辑</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                v-show="total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                :current-page="1"
                :page-sizes="[10, 20, 30, 40,50,100]"
                :page-size="10"
                layout="total, sizes, prev, pager, next, jumper"
                class="pagination-container"
                :total="total">
            </el-pagination>
    </el-card>
</template>

<script>
import {getCallbackList, getGroupList, getPluginList,remove} from '@/api/callback/config.api.js'
    export default {
        data() {
            return {
                queryParams: {},
                tableData: [],
                FrontendList: [],
                total: 0,
                // 网关分组
                gatewayGroupList: [],
                methods: ['GET','POST','PUT','DELETE','PATCH','HEAD','OPTIONS','TRACE'],
                pluginList: []
            }
        },
        filters: {
            groupFilter(value,gatewayGroupList) {
                return gatewayGroupList.find(item=>item.groupId===value).groupName
            }
        },
        mounted() {
            this.getList();
            this.getGroupList();
            this.getPluginList();
        },
        methods: {
            async getPluginList() {
                const result = await getPluginList();
                if(result.code === 200) {
                    this.pluginList = result.data;
                }
            },
            async getGroupList() {
                const result = await getGroupList();
                if(result.code === 200) {
                    this.gatewayGroupList = result.data;
                }
            },
            handleAdd() {
                this.$router.push({path: '/callback/detail/add'})
            },
            handleView(id) {
                this.$router.push({
                    path: '/callback/detail/view',
                    query: { originId: id }
                })
            },
            handleEdit(id) {
                this.$router.push({
                    path: '/callback/detail/edit',
                    query: { originId: id }
                })
            },
            handleDelete(id) {
                this.$confirm('确定将选择的数据删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const result = await remove(id);
                    if(result.code === 200) {
                            this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getList();
                    }else {
                        this.$message.error(result.data.msg);
                    }
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
              });
            },
            getList() {
                getCallbackList(this.queryParams).then(res=>{
                    if(res.code === 200) {
                        this.tableData = res.data.records;
                        this.total = res.data.total;
                    }
                })
            },
               // 搜索
            handleQuery() {
                if(this.queryParams.plugins.length > 0) {
                    this.queryParams.pluginId_in = this.queryParams.plugins.join(',')
                }  
                if(this.queryParams.methods.length > 0) {
                    this.queryParams.method_in = this.queryParams.methods.join(',')
                }   
                this.queryParams.current = 1;
                this.getList();
            },
            // 清空
            resetQuery(formName) {
                this.queryParams.plugins = [];
                this.queryParams.pluginId_in = '';
                this.queryParams.methods = [];
                this.queryParams.method_in = '';
                this.$refs[formName].resetFields();
                this.getList();
            },
            handleSizeChange(val) {
                this.queryParams.size = val;
                this.getList();
           },
            handleCurrentChange(val) {
                this.queryParams.current = val;
                this.getList();
            },
        }
    }
</script>
<style lang="scss" scoped>
.pagination-container {
    padding: 10px;
    text-align: right;
}
</style>